package com.hjwoo.app.adpter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hjwoo.app.R;

import org.xutils.image.ImageOptions;

import java.util.List;

public class TackPictureScanGridViewAdapter extends BaseAdapter {

	// 定义一个视图容器
	ViewHolder holder;
	private LayoutInflater inflater = null;
	private Context context;
	List<Bitmap> imgPathLS;
	ImageOptions imageOptions;
	private int num;

	public TackPictureScanGridViewAdapter(Context context,
										  List<Bitmap> imgPathLS,int num) {
		super();
		inflater = LayoutInflater.from(context);
		this.context = context;
		this.imgPathLS = imgPathLS;
		this.num = num;
		imageOptions = new ImageOptions.Builder()
//                        .setSize(DensityUtil.dip2px(120), DensityUtil.dip2px(120))//图片大小
//                        .setRadius(DensityUtil.dip2px(5))//ImageView圆角半径
				.setCrop(true)// 如果ImageView的大小不是定义为wrap_content, 不要crop.
				.setImageScaleType(ImageView.ScaleType.CENTER_CROP)
//                        .setLoadingDrawableId(null)//加载中默认显示图片
//                        .setFailureDrawableId(R.mipmap.ic_launcher)//加载失败后默认显示图片
				.build();
	}

	@Override
	public int getCount() {
		//最多选9张
		if (imgPathLS.size() == num) {
			return num;
		}
		return (imgPathLS.size() + 1);
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_takepicture_gv,
					parent, false);
			holder = new ViewHolder();
			holder.image = (ImageView) convertView
					.findViewById(R.id.item_takepicturescan_image);
			holder.item_delete_tv = (TextView) convertView
					.findViewById(R.id.item_delete_tv);
			convertView.setTag(holder);
		} else {
			// 取出holder
			holder = (ViewHolder) convertView.getTag();
		}

		if (position == imgPathLS.size()) {
			holder.image.setImageBitmap(BitmapFactory.decodeResource(
					context.getResources(), R.drawable.papers));
			if (position == 9) {
				holder.image.setVisibility(View.GONE);
			}
		} else {
			holder.image.setImageBitmap(imgPathLS.get(position));
		}
				return convertView;
	}

	/**
	 * 视图容器类
	 *
	 * @author kz
	 *
	 */
	public class ViewHolder {
		public ImageView image;
		public TextView item_delete_tv;
	}

}
